package com.lvwenguo.controller.echarts;

import com.google.gson.Gson;
import com.lvwenguo.model.EchartsDate;
import com.lvwenguo.service.serviceImple.EchartsDateImple;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/*
* 用于销售量和进货量的图表显示
*
* (这里只做了进货量的，销售量的没做)
* */
@WebServlet("/EchartsDateServlet")
public class EchartsDateServlet extends HttpServlet {
    EchartsDateImple echartsDateImple =new EchartsDateImple();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        List<List<EchartsDate>> lists = echartsDateImple.selectCountList();
        //创建list实现月的储存，和进行运算
        List<Object[]> list=new ArrayList<Object[]>();
        //进行计算后存入信息
        List<Integer> list2=new ArrayList<Integer>();
        //首先进行进货量的运算
        try {
            for(int i=0;i<=lists.get(0).size()+1;i++){
                if(i<lists.get(0).size()) {
           /*//取出年
            String.format("%ty",lists.get(0).get(i).getPurchase_date());*/
                    //取出月
                    String format = String.format("%tm", sdf.parse(lists.get(0).get(i).getPurchase_date()));
                    Object[] objects = {format, lists.get(0).get(i).getCount()};
                    list.add(objects);
            /*//取出日
            String.format("%td",lists.get(0).get(i).getPurchase_date());*/
                }
                else{
                    Object[] objects = {"", 0};
                    list.add(objects);
                }
        }
        //循环计算相同月的进货量
        int sum=0;
            int sum1=0;
            //为对比进行赋值
        String date=null;
        date=(String) list.get(1)[0];
        for(int i=0;i<list.size()-2;i++){
                if(list.get(i)[0].equals(date)){
                    if(list.get(i)[0].equals("12")){
                        sum=(Integer) list.get(i)[1]+sum;
                    }else{
                        sum=sum+(Integer) list.get(i)[1];
                        //sum=sum1+sum;
                        date = (String) list.get(i +2)[0];
                    }
                }else {
                    if (sum != 0) {
                        if(list.get(i)[0].equals("12")){
                            list2.add(sum+(Integer) list.get(i+1)[1]);
                            System.out.println(sum);
                            sum = 0;
                            sum1=0;
                        }else{
                            list2.add(sum+(Integer) list.get(i+1)[1]);
                            System.out.println(sum);
                            date = (String) list.get(i +2)[0];
                            sum = 0;
                        }
                    }else{
                        if(list.get(i)[0].equals("12")){
                            list2.add((Integer) list.get(i)[1]);
                        }else {
                            list2.add((Integer) list.get(i)[1]);
                            date = (String) list.get(i + 2)[0];
                             }
                    }
                }
        }
        //用于最后日期没有进入else
            list2.add(sum);
        Gson gson=new Gson();
        resp.getWriter().write(gson.toJson(list2));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
